package com.ruoyi.farmwell.fault.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.farmwell.fault.domain.WellFault;
import com.ruoyi.farmwell.fault.service.IWellFaultService;
import com.ruoyi.farmwell.ghy.domain.SysGhy;
import com.ruoyi.farmwell.well.domain.SysWell;
import com.ruoyi.farmwell.well.service.ISysWellService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机井故障表Controller
 * 
 * @author zjp
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/farmwell/fault")
public class WellFaultController extends BaseController
{
    @Autowired
    private IWellFaultService wellFaultService;
    @Autowired
    private ISysWellService sysWellService;
    /**
     * 查询机井故障表列表
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(WellFault wellFault)
    {
        startPage();
        List<WellFault> list = wellFaultService.selectWellFaultList(wellFault);
        return getDataTable(list);
    }
    @GetMapping("/queryByIsFault/{id}")
    public AjaxResult queryByIsFault(@PathVariable("id") Long id)
    {
        SysWell sysWell = sysWellService.selectSysWellById(id);
        WellFault wellFault=new WellFault();
        wellFault.setWellId(id);
        wellFault.setIsHandle("否");
        List<WellFault> list = wellFaultService.selectWellFaultList(wellFault);
        if (list.size()>0)
        {
            return AjaxResult.success(list.get(0));
        }
        return AjaxResult.error("没有故障数据");
    }
    /**
     * 导出机井故障表列表
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:export')")
    @Log(title = "机井故障表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WellFault wellFault)
    {
        List<WellFault> list = wellFaultService.selectWellFaultList(wellFault);
        ExcelUtil<WellFault> util = new ExcelUtil<WellFault>(WellFault.class);
        util.exportExcel(response, list, "机井故障表数据");
    }

    /**
     * 获取机井故障表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wellFaultService.selectWellFaultById(id));
    }

    /**
     * 新增机井故障表
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:add')")
    @Log(title = "机井故障表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WellFault wellFault)
    {
        SysWell sysWell = sysWellService.selectSysWellById(wellFault.getWellId());
        sysWell.setIsFault("是");
        sysWellService.updateSysWell(sysWell);
        wellFault.setReportTime(new Date());
        return toAjax(wellFaultService.insertWellFault(wellFault));
    }

    /**
     * 修改机井故障表
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:edit')")
    @Log(title = "机井故障表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WellFault wellFault)
    {
        wellFault.setIsHandle("是");
        SysWell sysWell = sysWellService.selectSysWellById(wellFault.getWellId());
        sysWell.setIsFault("否");
        sysWellService.updateSysWell(sysWell);
        return toAjax(wellFaultService.updateWellFault(wellFault));
    }

    /**
     * 删除机井故障表
     */
//    @PreAuthorize("@ss.hasPermi('farmwell:fault:remove')")
    @Log(title = "机井故障表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wellFaultService.deleteWellFaultByIds(ids));
    }

    @PostMapping("/reportfile")
    public AjaxResult fileUpload( MultipartFile file) throws Exception
    {

        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("filePath", filePath);
        return ajax;
    }
}
